package com.danskin.wechat.service.impl;

import java.math.BigDecimal;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.danskin.wechat.api.client.WeiXinOpenApiClient;
import com.danskin.wechat.dao.WxMsgTemplateDao;
import com.danskin.wechat.entity.WxMsgTemplate;
import com.danskin.wechat.service.WechatMsgTemplateService;
import com.danskin.wechat.util.DateUtils;
import com.danskin.wechat.util.PropertyUtil;

@Service
public class WechatMsgTemplateServiceImpl implements WechatMsgTemplateService {
	@Autowired
	public WxMsgTemplateDao msgTemplateDao;

	@Override
	public void sendCustBindMsg(String accessToken, String appId, String openId, String nickName, String mobile) {
		WxMsgTemplate msgTemplate = msgTemplateDao.selectByType(appId, "cust_bind");
		if (msgTemplate != null) {
			JSONObject data = new JSONObject();
			data.put("first", this.getPostData("亲爱的" + nickName + "，欢迎加入李宁CLUB"));
			data.put("keyword1", this.getPostData(mobile));
			data.put("keyword2", this.getPostData(DateUtils.formatTime()));
			data.put("remark", this.getPostData("了解更多会员信息，请点击详情。详情 >"));

			WeiXinOpenApiClient.createInstance().sendMsgTemplate(accessToken, openId, msgTemplate.getTemplateId(),
					PropertyUtil.getFrontUrl(appId, "home"), data);
		}
	}

	@Override
	public void sendCustSignupMsg(String accessToken, String appId, String openId, String nickName, String mobile,
			String memberId) {

		WxMsgTemplate msgTemplate = msgTemplateDao.selectByType(appId, "cust_signup");
		if (msgTemplate != null) {
			JSONObject data = new JSONObject();
			data.put("first", this.getPostData("亲爱的" + nickName + "，欢迎注册成为李宁CLUB微信会员"));
			data.put("keyword1", this.getPostData(mobile));
			data.put("keyword2", this.getPostData(memberId));
			data.put("keyword3", this.getPostData(DateUtils.formatTime()));
			data.put("remark", this.getPostData("谢谢您的支持，完善信息可以获得新人礼奖励哦。了解更多信息，请点击详情。详情 >"));

			WeiXinOpenApiClient.createInstance().sendMsgTemplate(accessToken, openId, msgTemplate.getTemplateId(),
					PropertyUtil.getFrontUrl(appId, "home"), data);
		}
	}

	@Override
	public void sendCustGetGoodsMsg(String accessToken, String appId, String openId, String custName,
			String consigneeName, String goodsName) {

		WxMsgTemplate msgTemplate = msgTemplateDao.selectByType(appId, "cust_get_goods");
		if (msgTemplate != null) {
			JSONObject data = new JSONObject();
			data.put("first", this.getPostData("亲爱的" + custName + "，您已经成功领取实物新人礼"));
			data.put("keyword1", this.getPostData(consigneeName));
			data.put("keyword2", this.getPostData(goodsName));
			data.put("keyword3", this.getPostData(DateUtils.formatTime()));
			data.put("remark", this.getPostData("谢我们会尽快发货，请于近期注意查收！"));

			WeiXinOpenApiClient.createInstance().sendMsgTemplate(accessToken, openId, msgTemplate.getTemplateId(), null,
					data);
		}
	}

	@Override
	public void sendCustGetCouponMsg(String accessToken, String appId, String openId, String custName,
			String couponName) {
		
//		WxMsgTemplate msgTemplate = msgTemplateDao.selectByType(appId, "cust_get_coupon");
//		if (msgTemplate != null) {
//			JSONObject data = new JSONObject();
//			data.put("first", this.getPostData("亲爱的" + custName + "，恭喜您获得一张优惠券！"));
//			data.put("keyword1", this.getPostData(custName));
//			data.put("keyword2", this.getPostData(couponName));
//			data.put("remark", this.getPostData("您可以前往就近李宁体育线下门店使用优惠券，了解更多信息，请点击详情。详情 >"));
//
//			WeiXinOpenApiClient.createInstance().sendMsgTemplate(accessToken, openId, msgTemplate.getTemplateId(),
//					PropertyUtil.getFrontUrl(appId, "coupon"), data);
//		}
	}

	@Override
	public void sendCustUseCouponMsg(String accessToken, String appId, String openId, String custName,
			String couponName) {

//		WxMsgTemplate msgTemplate = msgTemplateDao.selectByType(appId, "cust_use_coupon");
//		if (msgTemplate != null) {
//			JSONObject data = new JSONObject();
//			data.put("first", this.getPostData("亲爱的" + custName + "，您的" + couponName + "卡券已成功使用~"));
//			data.put("keyword1", this.getPostData(custName));
//			data.put("keyword2", this.getPostData("您的" + couponName + "卡券已使用"));
//			data.put("remark", this.getPostData("如有疑问请资料客服电话400-610-0011"));
//
//			WeiXinOpenApiClient.createInstance().sendMsgTemplate(accessToken, openId, msgTemplate.getTemplateId(), null,
//					data);
//		}
	}

	@Override
	public void sendCustChangePointsMsg(String accessToken, String appId, String openId, String custName, String mobile,
			int changePoints, int avaPoints) {
//		if (changePoints >= 0) {
//			return;// 获得积分暂时屏蔽，为正数
//		}
//		WxMsgTemplate msgTemplate = msgTemplateDao.selectByType(appId, "cust_change_points");
//		if (msgTemplate != null) {
//			JSONObject data = new JSONObject();
//			data.put("first", this.getPostData("亲爱的" + custName + "，您的积分账户变更如下"));
//			data.put("FieldName", this.getPostData("手机号码", "#000"));
//			data.put("Account", this.getPostData(mobile));
//			data.put("change", this.getPostData((changePoints < 0 ? "扣减" : "增加"), "#000000"));
//			data.put("CreditChange", this.getPostData(Math.abs(changePoints)));
//			data.put("CreditTotal", this.getPostData(avaPoints));
//			data.put("Remark", this.getPostData("积分可以兑换丰富好礼哦。点击查看更多积分好礼！详情 >"));
//
//			WeiXinOpenApiClient.createInstance().sendMsgTemplate(accessToken, openId, msgTemplate.getTemplateId(),
//					PropertyUtil.getFrontUrl(appId, "mall"), data);
//		}
	}

	@Override
	public void sendCustPointGetItemMsg(String accessToken, String appId, String openId, String custName, String mobile,
			String itemName, int quantity, int totalPoints) {

//		WxMsgTemplate msgTemplate = msgTemplateDao.selectByType(appId, "points_get_item");
//		if (msgTemplate != null) {
//			JSONObject data = new JSONObject();
//			data.put("first", this.getPostData("您好，您使用积分兑换礼品成功！"));
//			data.put("keyword1", this.getPostData(custName));
//			data.put("keyword2", this.getPostData(mobile));
//			data.put("keyword3", this.getPostData(itemName));
//			data.put("keyword4", this.getPostData(quantity));
//			data.put("keyword5", this.getPostData(totalPoints));
//			data.put("remark", this.getPostData("感谢您的参与！"));
//
//			WeiXinOpenApiClient.createInstance().sendMsgTemplate(accessToken, openId, msgTemplate.getTemplateId(),
//					PropertyUtil.getFrontUrl(appId, "mall"), data);
//		}
	}

	@Override
	public void sendCustJoinApplyMsg(String accessToken, String appId, String openId, String activityTitle,
			Date beginTime, Date endTime, String address, String description) {

		// {{first.DATA}}
		// 活动名称：{{keyword1.DATA}}
		// 活动时间：{{keyword2.DATA}}
		// 活动地点：{{keyword3.DATA}}
		// 报名详情：{{keyword4.DATA}}
		// {{remark.DATA}}
		WxMsgTemplate msgTemplate = msgTemplateDao.selectByType(appId, "cust_join_apply");
		if (msgTemplate != null) {
			JSONObject data = new JSONObject();
			data.put("first", this.getPostData("尊敬的用户，您已成功报名该活动。"));
			data.put("keyword1", this.getPostData(activityTitle));
			data.put("keyword2",
					this.getPostData(DateUtils.formatTime(beginTime) + "至" + DateUtils.formatTime(endTime)));
			data.put("keyword3", this.getPostData(address));
			data.put("keyword4", this.getPostData(description));
			data.put("remark", this.getPostData("更多活动信息请关注官方微信公众号"));
			// TODO:地址未确认
			WeiXinOpenApiClient.createInstance().sendMsgTemplate(accessToken, openId, msgTemplate.getTemplateId(), null,
					data);
		}
	}

	@Override
	public void sendCustJoinVoteMsg(String accessToken, String appId, String openId, String activityTitle,
			Date joinTime) {

		// {{first.DATA}}
		// 投票标题：{{keyword1.DATA}}
		// 创建时间：{{keyword2.DATA}}
		// {{remark.DATA}}
		WxMsgTemplate msgTemplate = msgTemplateDao.selectByType(appId, "cust_join_vote");
		if (msgTemplate != null) {
			JSONObject data = new JSONObject();
			data.put("first", this.getPostData("恭喜您投票成功！"));
			data.put("keyword1", this.getPostData(activityTitle));
			data.put("keyword2", this.getPostData(DateUtils.formatTime(joinTime)));
			data.put("remark", this.getPostData("感谢您宝贵的一票，点击查看投票结果！详情 >"));

			WeiXinOpenApiClient.createInstance().sendMsgTemplate(accessToken, openId, msgTemplate.getTemplateId(),
					PropertyUtil.getFrontUrl(appId, "vote"), data);
		}
	}

	@Override
	public void sendCustPayOrderMsg(String accessToken, String appId, String openId, String custName, String orderNo,
			BigDecimal amount, String storeName, Date payTime) {

		// {{first.DATA}}
		// 订单编号：{{keyword1.DATA}}
		// 消费金额：{{keyword2.DATA}}
		// 消费门店：{{keyword3.DATA}}
		// 消费时间：{{keyword4.DATA}}
		// {{remark.DATA}}
		WxMsgTemplate msgTemplate = msgTemplateDao.selectByType(appId, "cust_pay_order");
		if (msgTemplate != null) {
			JSONObject data = new JSONObject();
			data.put("first", this.getPostData("亲爱的会员" + custName + "，您的订单已支付成功"));
			data.put("keyword1", this.getPostData(orderNo));
			data.put("keyword2", this.getPostData(amount));
			data.put("keyword3", this.getPostData(storeName));
			data.put("keyword4", this.getPostData(DateUtils.formatTime(payTime)));
			data.put("remark", this.getPostData("点击详情查看所有消费记录！详情 >"));

			WeiXinOpenApiClient.createInstance().sendMsgTemplate(accessToken, openId, msgTemplate.getTemplateId(),
					PropertyUtil.getFrontUrl(appId, "expense"), data);
		}
	}

	private JSONObject getPostData(Object value) {

		return this.getPostData(value, "#173177");
	}

	private JSONObject getPostData(Object value, String color) {

		JSONObject postData = new JSONObject();
		postData.put("value", value);
		postData.put("color", color);

		return postData;
	}
}
